import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpEchoClient {

public DatagramSocket socket=null;
String serverIP="";
int serverPort=0;
public UdpEchoClient(String ip,int port) throws SocketException {
    socket=new DatagramSocket();
    serverIP=ip;
    serverPort=port;
}
public void start() throws IOException {
    System.out.println("客户端启动");
    Scanner scanner=new Scanner(System.in);
    while (true){
;        String request= scanner.next();
        DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length, InetAddress.getByName(serverIP),serverPort);
socket.send(requestPacket);
//读取服务器返回的响应
        DatagramPacket responsePacket=new DatagramPacket(new byte[4096],4096);
        socket.receive(responsePacket);
        //把相应转换为字符串并显示出来
        String response=new String(responsePacket.getData(),0,responsePacket.getLength());
        System.out.println(response);
    }
}

    public static void main(String[] args) throws IOException {
        UdpEchoClient client=new UdpEchoClient("127.0.0.1", 9090);
        client.start();
    }

}
